<template>
  <div @keyup.enter="getData()">
    <el-form class="query-form" :inline="true" :model="params">
      <el-form-item>
        <el-input v-model="params.census_number" placeholder="编号查询" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-input v-model="params.load_main_entity" placeholder="主体名称查询" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getData()">
          <i class="fa fa-fw fa-search"></i>
          查询
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogImportVisible = true">
          <i class="fa fa-fw fa-upload"></i>
          {{ importTitle }}
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData"></c-table>

    <div class="query-pagination">
      <el-pagination
        @current-change="getData"
        :current-page.sync="params.index"
        :page-size="params.size"
        layout="total, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>

    <import-excel
      :visible.sync="dialogImportVisible"
      :title="importTitle"
      :post-api="postUrl"
      :excel-template="excelTemplateUrl"
      @success="getData"
    ></import-excel>
  </div>
</template>
<script>
import ImportExcel from '@/components/ImportExcel'
import { mapGetters } from 'vuex'
import { queryTrouble } from '@vimi/utils-api'

export default {
  components: {
    ImportExcel,
  },
  props: {},
  data() {
    return {
      postUrl: '',
      importTitle: '导入调查报告(简易版)',
      excelTemplateUrl: '/static/files/调查报告(简易版)导入模板.xlsx',
      dialogImportVisible: false,

      loading: false,
      params: {
        index: 1,
        size: 10,
        census_number: '',
        load_main_entity: '',
        'service_id.eq': '',
      },
      tableName: 'credit_census_report_simple',
      tableData: [],
      totalCount: 0,
      tableColumns: [
        { attrs: { label: '尽调编号', prop: 'census_number' } },
        { attrs: { label: '借款主体', prop: 'load_main_entity' } },
        { attrs: { label: '担保期限', prop: 'warranty_period' } },
        { attrs: { label: '调查日期', prop: 'survey_date' } },
        { attrs: { label: '申请人基本信息', prop: 'base_info' } },
        { attrs: { label: '经营情况', prop: 'business_des', minWidth: '200px' } },
        { attrs: { label: '担保贷款用途调查', prop: 'borrowing_reason', minWidth: '200px' } },
        { attrs: { label: '担保贷款用途调查', prop: 'borrowing_reason', minWidth: '200px' } },
        { attrs: { label: '申请人还款能力分析', prop: 'repayment_plan', minWidth: '200px' } },
        {
          attrs: {
            label: '反担保措施评价',
            prop: 'counter_guarantee_evaluation',
            minWidth: '200px',
          },
        },
        {
          attrs: { label: '风险评价及调查结论', prop: 'project_risk_analysis', minWidth: '200px' },
        },
        { attrs: { label: '调查员A角', prop: 'res_a_name' } },
        { attrs: { label: '调查员A角意见', prop: 'a_opinion', minWidth: '200px' } },
        { attrs: { label: '调查员B角', prop: 'res_b_name' } },
        { attrs: { label: '调查员B角意见', prop: 'b_opinion', minWidth: '200px' } },
        {
          attrs: {
            label: '有权审批人意见(信贷负责人意见)',
            prop: 'approval_option',
            minWidth: '200px',
          },
        },
        { attrs: { label: '贷款人身份证号', prop: 'lender_idcard' } },
      ],
    }
  },
  computed: {
    ...mapGetters(['clientId', 'job']),
    superJob() {
      return [
        'org_credit_super',
        'org_c_manage',
        'org_r_manage',
        'org_p_manage',
        'org_c_general',
      ].includes(this.job)
    },
  },
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.postUrl =
      '/api/credit/import/' +
      queryTrouble(this.tableName, {
        importKey: `${this.tableName}_template`,
        'service_id.eq': this.clientId,
        'customer_related_id.eq': 'relatedId',
      })

    this.getData()
  },
  mounted() {},
  methods: {
    async getData() {
      this.loading = true
      let [res, err] = await this.$utils.promise(
        this.$api.getData('credit', this.tableName, this.params, { create_time: 'desc' })
      )
      this.loading = false

      if (err) {
        return this.$message.error('获取数据失败，请稍后重试！')
      }

      const data = (res && res.data) || {}
      this.tableData = data.data || []
      this.totalCount = data.totalCount || 0
    },
  },
}
</script>
<style scoped></style>
